package com.hu.common.util.user;

import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;

import java.util.Date;

/**
 *
 * @author RDP开发平台组
 * @version V3.1.0
 * @copyright 信息技术有限公司
 * @date 2021/3/16 8:49
 */
public class JwtsUtil {

    /**
     * 获取jwt中的携带的Redis的token
     * @param token
     * @return
     */
    public static String getRealToken(String token) {
        String realToken;
        try {
            SignedJWT sjwt = SignedJWT.parse(token.split(" ")[1]);
            JWTClaimsSet claims = sjwt.getJWTClaimsSet();
            realToken =  String.valueOf(claims.getClaim("token"));
            return realToken;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取jwt中的携带的Redis的token
     * @param token
     * @return
     */
    public static Integer getSingleLogin(String token) {
        int singleLogin;
        try {
            SignedJWT sjwt = SignedJWT.parse(token.split(" ")[1]);
            JWTClaimsSet claims = sjwt.getJWTClaimsSet();
            //singleLogin = (int) claims.getClaim("singleLogin");
            singleLogin = ((Number) claims.getClaim("singleLogin")).intValue();
            return singleLogin;
        } catch (Exception e) {
            return singleLogin = 1;
        }
    }

    /**
     * 获取jwt中的过期时间
     * @param token
     * @return
     */
    public static Date getExp(String token){
        Date date;
        try {
            SignedJWT sjwt = SignedJWT.parse(token.split(" ")[1]);
            JWTClaimsSet claims = sjwt.getJWTClaimsSet();
            date = (Date)claims.getClaim("exp");
            return date;
        } catch (Exception e) {
            return date = null;
        }
    }

    public static void main(String[] args) {
        Integer test = getSingleLogin("bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJzaW5nbGVMb2dpbiI6MSwiZXhwIjoxNjk0Nzk5OTA0LCJhdXRob3JpdGllcyI6WyJpc0FkbWluIl0sImp0aSI6InNfT3dtSkM3aG8weWhKVlctSVh6MF9hWmMzayIsImNsaWVudF9pZCI6ImFkbWluIiwidG9rZW4iOiJsb2dpbl90b2tlbl80NzIzNjM3ODU3ODEyNDg3MDkifQ.ApyyiF5en7irb5waQdkBhPLRgRyp3_Ay61m3vQULNCO5Gv7O-LuCEyCDwo1yXT7k-guXh_Z-IqJPlIO9jdHrzzikFaJBKO-oMjsuzeQBgs66-HC_szLVmBchFtYDSOLAiW42rWkW0Zi0oGvUh0KZ_AfOHE1hcVnrkS6qouf4ZAiuFSBYdoiRIsIghj7QCxdpe6Wp8cRgQNEoxYROQUA_TXuvEoS6FaVEg19pHd_8-0O_sbearwUD1LI5JMU127zOz_Todv6Xqva4Pd4YFkAVhupgPkgq6sxUYeHIwBKdQIye8slR2qWfaJL5WufvMWugVLfJXhv6dl8SQZs-Vg_RGw");
        System.out.println(test);
    }


    public static String createCustomToken(String username, String realToken){
        try{
            RSAKey rsaJWK = new RSAKeyGenerator(2048)
                .keyID("hmp")
                .generate();
            rsaJWK.toPublicJWK();
            // Create RSA-signer with the private key
            JWSSigner signer = new RSASSASigner(rsaJWK);
            // Prepare JWT with claims set
            JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
                    .subject(username)
                    .expirationTime(new Date(System.currentTimeMillis() + 12 * 60 * 60 * 1000))
                    .claim("token", realToken)
                    .build();

            SignedJWT signedJWT = new SignedJWT(
                    new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(rsaJWK.getKeyID()).build(),
                    claimsSet);

            // Compute the RSA signature
            signedJWT.sign(signer);
            return signedJWT.serialize();
        } catch (Exception e) {
            return null;
        }
    }
}
